﻿
$(function () {
    //login-register dialog
    var loginId = $("#loginId"),
        loginPassword = $("#loginPassword"),
        allFields = $([]).add(loginId).add(loginPassword),
        tips = $(".validateTips");

    var registerId = $("#registerId"),
        registerPassword = $("#registerPassword"),
        registerPasswordConfirm = $("#registerPasswordConfirm"),
        registerName = $("#registerName"),
        allFieldsRegister = $([]).add(registerId).add(registerPassword).add(registerPasswordConfirm).add(registerName);

    function updateTips(t) {
        tips.text(t).addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " + min + " and" + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#login-dialog").dialog({
        autoOpen: false,
        height: 500,
        width: 300,
        modal: true,
        resizable: false,
        buttons: {
            "Login": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(loginId, "username", 3, 16);
                bValid = bValid && checkLength(loginPassword, "password", 5, 16);

                bValid = bValid && checkRegexp(loginId, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");
                bValid = bValid && checkRegexp(loginPassword, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9");

                if (bValid) {
                    //khung chat
                    loginHandler();
                    $(this).dialog("close");   
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#register-dialog").dialog({
        autoOpen: false,
        height: 650,
        width: 300,
        modal: true,
        resizable: false,
        buttons: {
            "Register": function () {
                var bValid = true;
                allFieldsRegister.removeClass("ui-state-error");

                bValid = bValid && checkLength(registerId, "username", 3, 16);
                bValid = bValid && checkLength(registerPassword, "password", 5, 16);
                bValid = bValid && checkLength(registerPasswordConfirm, "password confirm", 5, 16);
                bValid = bValid && checkLength(registerName, "name", 3, 20);

                bValid = bValid && checkRegexp(registerId, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");
                bValid = bValid && checkRegexp(registerPassword, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9");
                bValid = bValid && checkRegexp(registerPasswordConfirm, /^([0-9a-zA-Z])+$/, "Password confirm field only allow : a-z 0-9");
                bValid = bValid && checkRegexp(registerName, /^[a-zA-Z]/, "Full Name field only allow : a-z A-Z");

                if (bValid) {
                    //khung chat
                    registerHandler();
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#loginButton").click(function() {
        $("#login-dialog").dialog("open");
    });

    $("#registerButton").click(function () {
        $("#register-dialog").dialog("open");
    });

    
    function loginHandler() {
        var username = $("#loginId").val();
        //alert(username);
        var password = $("#loginPassword").val();
        //alert(password);

        $.post("http://localhost:64395/api/Login/Post/", { UserName: username, Password: password }, function (data) {
            //alert(JSON.stringify(data));
            if (data != null) {
                var userId = username;
                //window.location.href = "/chat.html?userId="+userId;
                $.cookie('username', username, { expires: 14 });
                window.location.href = "/chat.html";
            }else {
                alert("Please login again!");
            }
        });
    }

    function registerHandler() {
        var username = $("#registerId").val();
        var password = $("#registerPassword").val();
        var repassword = $("#registerPasswordConfirm").val();
        var fullname = $("#registerName").val();
        var sex = $("#registerSex").val();
        if (password == repassword) {
            $.post("http://localhost:64395/api/Register/Post/", { UserName: username, Password: password, FullName: fullname, Sex: sex }, function(data) {
                if (data != null) {
                    //alert(JSON.stringify(data));
                    $.cookie('username', username, { expires: 14 });
                    window.location.href = "/chat.html";
                } else {
                    alert("Register fail!");
                }
            });
        }else {
            alert("Password retype and password different!");
        }
    }
});